#define _CRT_SECURE_NO_WARNINGS



#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int a[N], cnt[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        cnt[a[i]]++;
    }
    if (cnt[0] == n)
        cout << 0 << endl;
    else
    {
        int ans = 0;
        while (cnt[ans])
            ans++;
        for (int i = 1; i <= n; i++)
        {
            if (a[i] == 0)
            {
                ans = max(ans, a[i - 1]);
                ans = max(ans, a[i + 1]);
            }
        }
        cout << ans << endl;
    }
    return 0;
}
